package com.example.carbon.controller;

import com.example.carbon.pojo.dto.DeviceInfoSearch;
import com.example.carbon.pojo.dto.DeviceInfoSetData;
import com.example.carbon.pojo.dto.PageLimit;
import com.example.carbon.pojo.vo.RestBean;
import com.example.carbon.server.info.DeviceServer;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 用于设备信息的controller层
 */
@RestController
@RequestMapping("api/device")
@Slf4j
public class DeviceDataController {
    @Resource
    DeviceServer deviceServer;

    /**
     * 通过restful风格来进行数据的读取，然后主要传参有page,item,order
     * page用于页数，默认为0
     * item用于每页个数 默认为10
     * order为排序字段，默认id降序
     *
     * @param page
     * @param item
     * @return DeviceShowData
     */
    @PostMapping("/info/{page}/{item}")
    public RestBean getTableData(@PathVariable(value = "page") int page, @PathVariable(value = "item") int item,
                                 @RequestBody DeviceInfoSearch deviceInfoSearch) {
        log.info(deviceInfoSearch.toString());
        PageLimit pageLimit = new PageLimit(page, item);
        log.info(deviceInfoSearch.toString());
        try {
            return RestBean.success(deviceServer.searchInfo(deviceInfoSearch, pageLimit));
        } catch (Exception e) {
            log.info(e.getMessage());
            return RestBean.failure("获取数据失败");
        }
    }

    @PostMapping("/update")
    public RestBean updateDeviceInfo(@RequestBody DeviceInfoSetData data) {
        try {
            if (deviceServer.updateDeviceInfo(data)) {
                return RestBean.success();
            }
            return RestBean.failure("更新失败");
        } catch (Exception e) {
            log.info(e.getMessage());
            return RestBean.failure("服务器故障，请联系管理员");
        }
    }

    //通过id来获取对象的信息
    @GetMapping("/update/info/{id}")
    public RestBean getUpdateSettingInfoById(@PathVariable(value = "id") int id) {
        try {
            return RestBean.success(deviceServer.getDeviceSettingInfoByID(id));
        } catch (Exception e) {
            log.info(e.getMessage());
            return RestBean.failure("获取信息失败");
        }
    }

    @RequestMapping("/del/{id}")
    public RestBean delInfoById(@PathVariable(value = "id") int id) {
        try {
            if (deviceServer.deleteDeviceInfo(id)) {
                return RestBean.success("删除成功");
            }
            return RestBean.failure("删除失败");
        } catch (Exception e) {
            log.info(e.getMessage());
            return RestBean.failure("服务器处理失败，请联系管理员");
        }
    }

    @PostMapping("/insert")
    public RestBean insertNewInfo(@RequestBody DeviceInfoSetData data) {
        try {
            if (deviceServer.insertNewInfo(data)) {
                return RestBean.success("插入成功");
            }
            return RestBean.failure("插入失败");
        } catch (Exception e) {
            log.info(e.getMessage());
            return RestBean.failure("服务器处理失败，请联系管理员");
        }
    }

}
